package com.kjtpay.design_pattern.bridge;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class BridgeActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, BridgeActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    RadioButton brandM;
    RadioButton brandN;
    RadioButton game;
    RadioButton addressList;
    RadioButton mp3;
    TextView messageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bridge);
        setTitle("桥接模式");

        brandM = findViewById(R.id.radioButtonBrandM);
        brandN = findViewById(R.id.radioButtonBrandN);
        game = findViewById(R.id.radioButtonGame);
        addressList = findViewById(R.id.radioButtonAddressList);
        mp3 = findViewById(R.id.radioButtonMP3);

        messageView = findViewById(R.id.textViewMessage);
    }

    public void onRunClick(View view) {
        HandsetBrand brand = null;
        HandsetSoft soft = null;

        // 确定选了哪个手机品牌
        if (brandM.isChecked()) {
            brand = new HandsetBrandM();
        } else if (brandN.isChecked()) {
            brand = new HandsetBrandN();
        } else {
            // 未定义，什么也不做
        }

        // 确定选了哪个软件
        if (game.isChecked()) {
            soft = new HandsetGame();
        } else if (addressList.isChecked()) {
            soft = new HandsetAddressList();
        } else if (mp3.isChecked()) {
            soft = new HandsetMP3();
        }

        // 组合好了，运行
        if ((null != brand) && (null != soft)) {
            brand.setSoft(soft);
            messageView.setText(brand.run());
        }

    }
}